
local MainScene = class("MainScene", function()
    --return display.newScene("MainScene")
    return display.newPhysicsScene("MainScene")
end)

local TestNode = class('TestNode',function()
    return display.newNode('TestNode')
end)

local TestNode2 = class('TestNode2',TestNode)

function TestNode:ctor()
    print('... this is TestNode:ctor ...')
    function self.fun() 
        print('... this is TestNode:self.fun ...')
    end
end

function TestNode2:ctor()
    TestNode2.super.ctor(self)
    print('... this is TestNode2:ctor ...')
    if not self.fun then print('not have fun')
    else print(' have fun')
    end
end


function MainScene:ctor()
    cc.ui.UILabel.new({
            UILabelType = 2, text = "Hello, World", size = 64})
        :align(display.CENTER, display.cx, display.cy)
        :addTo(self)

    local textrue = cc.Director:getInstance():getTextureCache():addImage('iori.png')
    local frames = {}
    for i=1,5 do
    	for j=1,16 do
    		local f = cc.SpriteFrame:createWithTexture(textrue,cc.rect(192*(i-1),192*(j-1),192,192))
            f.name = 'i .. j'
    		frames[#frames +1] = f
    	end
    end

    local frames_run = {}
    for i=19,26 do
    	frames_run[#frames_run + 1] = frames[i]
    end
    local frames_jmp = {}
    for i=55,68 do
    	frames_jmp[#frames_jmp + 1] = frames[i]
    end

    local sprite_run = cc.Sprite:createWithSpriteFrame(frames_run[1]):addTo(self)
    sprite_run:setTag(10)
    sprite_run:pos(display.cx,display.cy)
    sprite_run:playAnimationForever(display.newAnimation(frames_run, 0.8/#frames_run))
    local sprite_jmp = cc.Sprite:createWithSpriteFrame(frames_jmp[1]):addTo(self)
    sprite_jmp:setTag(11)
    sprite_jmp:pos(display.cx,display.cy-200)
    sprite_jmp:playAnimationForever(display.newAnimation(frames_jmp, 0.8/#frames_jmp),0.2)
    local frames_run_jmp = clone(frames_run)
    table.insertto(frames_run_jmp,frames_jmp)

    local sprite_run_jmp = cc.Sprite:createWithSpriteFrame(frames_run_jmp[1]):addTo(self)
    sprite_run_jmp:pos(display.cx,display.cy+100)
    sprite_run_jmp:setTag(12)

    local seq = cc.Sequence:create(
    	cc.Animate:create(display.newAnimation(frames_run, 0.8/#frames_run)),
    	cc.CallFunc:create(function()
    		sprite_run_jmp:pos(display.cx+50,display.cy+100+80)
    		end),
    	cc.Animate:create(display.newAnimation(frames_jmp, 0.8/#frames_jmp)),
    	cc.CallFunc:create(function()
    		sprite_run_jmp:pos(display.cx,display.cy+100)
    		end)
    	)
    sprite_run_jmp:runAction(cc.RepeatForever:create(seq))


    self.world = self:getPhysicsWorld()
    self.world:setGravity(cc.p(0, 0))
    self.world:setDebugDrawMask(cc.PhysicsWorld.DEBUGDRAW_ALL)

    local sizeOrigin = sprite_run_jmp:getContentSize()
    local size = cc.size(130,100)
    local myBody = cc.PhysicsBody:createBox(size,cc.PHYSICSBODY_MATERIAL_DEFAULT,cc.p(-31,46))
    --local myBody = cc.PhysicsBody:createBox(size)
    local shap = cc.PhysicsShapeBox:create(sizeOrigin)
    --myBody:addShape(shap)
    --local myBody = cc.PhysicsBody:createBox(size)
    sprite_run_jmp:setPhysicsBody(myBody)

    local a = sprite_run_jmp:getAnchorPoint()
    print(a.x,a.y)

    local myBody2 = cc.PhysicsBody:createBox(size,cc.PHYSICSBODY_MATERIAL_DEFAULT,cc.p(-31,46))
    sprite_run:setPhysicsBody(myBody2)


    local shape2 = cc.PhysicsShapeBox:create(cc.size(80,90))
    local body3 = cc.PhysicsBody:create()
    body3:addShape(shape2)
    if not body3 then print('... not have body3 ...') end
    local jmp_node = display.newNode():addTo(sprite_jmp)
    jmp_node:setPhysicsBody(body3)
    jmp_node:pos(0,30)
    display.newSolidCircle(5, {color = cc.c4f(1.0,1.0,0.2,1.0)}):addTo(jmp_node):pos(0,0)
    jmp_node:setAnchorPoint(cc.p(0,0))
    body3:setPositionOffset(cc.p(40,41))
    --local jmp_scaleY = cc.ScaleBy:create(0.4,1.0,2)
    --local jmp_scaleY2 = cc.ScaleBy:create(0.4,1.0,0.5)
    --local jmp_seq = cc.Sequence:create(jmp_scaleY,jmp_scaleY2)
    local jmp_moveBy1 = cc.MoveBy:create(0.4,cc.p(0,40))
    local jmp_moveBy2 = cc.MoveBy:create(0.4,cc.p(0,-40))
    local jmp_seq = cc.Sequence:create(jmp_moveBy1,jmp_moveBy2,cc.DelayTime:create(0.2))
    jmp_node:runAction(cc.RepeatForever:create(jmp_seq))
    --sprite_jmp:setPhysicsBody(body3)

    local layer = display.newLayer():addTo(self)
    local circle = display.newSolidCircle(20, {color = cc.c4f(0.5,0.5,0.6,0.6)}):addTo(self)
    layer:setTouchEnabled(true)
    layer:addNodeEventListener(cc.NODE_TOUCH_EVENT, function(event)
        if event.name == 'began' then
            --print('... onTouch ...',event.x,event.y)
            local p = cc.p(event.x,event.y)
            local pn = circle:convertToNodeSpace(p)
            if pn.x > 20 or pn.x < -20 or pn.y > 20 or pn.y <-20 then
                return false
            else
                return true
            end
        elseif event.name == 'moved' then
            local dx = event.x - event.prevX
            local dy = event.y - event.prevY
            local x = circle:getPositionX() + dx
            local y = circle:getPositionY() + dy
            circle:pos(x,y)
            return true
        else
            return true
        end
    end)
    local bodyCircle = cc.PhysicsBody:createCircle(20)
    circle:setPhysicsBody(bodyCircle)

    local dispatcher = cc.Director:getInstance():getEventDispatcher()
    --local listener = cc.EventListenerPhysicsContactWithBodies:create(body3,bodyCircle) 
    --local listener = cc.EventListenerPhysicsContact:create()
    local listener = cc.EventListenerPhysicsContactWithGroup:create(1)
    body3:setContactTestBitmask(3)
    bodyCircle:setContactTestBitmask(3)
    myBody2:setContactTestBitmask(3)
    myBody:setContactTestBitmask(3)
    --body3:setCollisionBitmask(2)
    --bodyCircle:setCollisionBitmask(4)
    --body3:setCategoryBitmask(1)
    --bodyCircle:setCategoryBitmask(1)
    --myBody2:setContactTestBitmask(3)
    body3:setGroup(1)
    bodyCircle:setGroup(1)
    myBody2:setGroup(2)

    function list(contain)
        print('list')
        local spa = contain:getShapeB()
        local body = spa:getBody()
        local node = body:getNode()
        local tag = node:getTag()
        local sprite = display.getRunningScene():getChildByTag(tag)
        print('tag  =',tag)
        local f = sprite:getSpriteFrame()
        if not f then print('not have f ') end
        print(f.name)
        return true
    end
    function list2()
        print('list2')
    end
    ---[[
    listener:registerScriptHandler(list,cc.Handler.EVENT_PHYSICS_CONTACT_BEGIN)
    --]]
    dispatcher:addEventListenerWithFixedPriority(listener,5)

    --local tstNode = display.newNode()
    local tstNode = display.newSolidCircle(50, {color = cc.c4f(1.0,0.5,0.6,1.0)}):addTo(self)
    tstNode:pos(50,200) 
    local tstBody = cc.PhysicsBody:createBox(cc.size(100,100))
    tstBody:setGroup(1)
    --tstBody:setContactTestBitmask(3)
    tstNode:setPhysicsBody(tstBody)
    
    local scale1 = cc.ScaleBy:create(2,0.5,0.5)
    local scale2 = cc.ScaleBy:create(2,2,2)
    local seq2 = cc.Sequence:create(scale1,scale2)
    tstNode:runAction(cc.RepeatForever:create(seq2))

    local myNode = TestNode2:new()
    myNode:addTo(self)
    myNode:pos(100,100)
    display.newSolidCircle(20, {color = cc.c4f(0.6,1.5,0.8,1.0)}):addTo(myNode)

end

function MainScene:onEnter()
end

function MainScene:onExit()
end

return MainScene
